package com.tongguan.paypos.adapter;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tongguan.paypos.R;

import java.util.ArrayList;

/**
 * Created by WangChang on 2016/4/1.
 */
public class RechargeAdapter extends RecyclerView.Adapter<RechargeAdapter.BaseViewHolder> {

    private ArrayList<Double> dataList = new ArrayList<>();
    private int lastPressIndex = -1;
    private MyItemClickListener mItemClickListener;

    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    public void replaceAll(ArrayList<Double> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.one, parent, false), mItemClickListener);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.setData(dataList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(Double data) {
        }
    }

    public void replaceItemClickListener() {
        if ( lastPressIndex != -1) {
            lastPressIndex = -1;
            notifyDataSetChanged();
        }
    }

    /**
     * 设置Item点击监听
     *
     */
    public void setOnItemClickListener(MyItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    private class OneViewHolder extends BaseViewHolder {
        private TextView tv;
        private MyItemClickListener mListener;

        public OneViewHolder(View view, MyItemClickListener listener) {
            super(view);
            tv = (TextView) view.findViewById(R.id.tv);
            mListener = listener;
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (lastPressIndex == position) {
                    lastPressIndex = -1;
                } else {
                    lastPressIndex = position;
                }
                if (mListener != null) {
                    mListener.onItemClick(v, lastPressIndex);
                }
                notifyDataSetChanged();
            });
        }

        @Override
        void setData(Double data) {
            if (data != null) {
                double i = data;
                String text = (int) i + "元";
                tv.setText(text);
                if (getAdapterPosition() == lastPressIndex) {
                    tv.setSelected(true);
                    tv.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.white));
                } else {
                    tv.setSelected(false);
                    tv.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.blue_500));
                }
            }
        }

    }
}
